/****************************************************************
*								*
*  Parse command line						*
*								*
****************************************************************/

#include <assert.h>
#include <getopt.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <string.h>
using namespace std;

/* Default values */
#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif

const int def_N = 32;
const int def_niter = 5;
const int def_check = FALSE;
const int def_nocomm = FALSE;

#define MATCH(s) (!strcmp(argv[arg], (s)))
int PrintUsage(const char *const program, const char *const option);

/****************************************************************
*								*
* Parse the parameters from argv 				*
*								*
*	-n	<integer>	matrix size			*
*	-i	<integer>	# iterations 			*
*	-c			check the results		*
*	-nocomm			shut off communication          *
*								*
*****************************************************************/

void cmdLine(int argc, char **argv, int& N, int& niter, int& fcheck, int& nocomm)
{
   /* Default values */
   N = def_N;
   niter = def_niter;
   fcheck = def_check;
   nocomm = def_nocomm;

 static struct option long_options[] = {
        {"n", required_argument, NULL, 'n'},
        {"iter", required_argument, NULL, 'i'},
        {"check", no_argument, NULL, 'c'},
        {"nocomm", no_argument, NULL, 'k'},
 };

 // Parse the command line arguments
    int c;
    while ((c=getopt_long(argc,argv,"n:i:ck?",long_options,NULL)) != -1){
        switch (c) {

	    // Matrix dimension
            case 'n':
                N = atoi(optarg);
                break;

	    // # iterations
            case 'i':
                niter = atoi(optarg);
                break;

	    // Check solution?
            case 'c':
                fcheck = 1;
                break;

	    // Disable communication?
            case 'k':
                nocomm = 1;
                break;

	    // Error
            case '?':
	       cout << "Error in command line argument: " << optarg << endl;
	       cout << "\t-n <integer> matrix size [default: 32]\n";
	       cout << "\t-i <integer> # iterations [default: 5]\n";
	       cout << "\t-c           check solution? [default: No]\n";
	       cout << "\t-k           disable communication? [default: No]\n";
	       cout << flush;
               exit(-1);
	       break;

            default:  /* You can't get here */
	       break;
	}
     }
}
